<?php
//by willson 105328468@qq.com 2014/9/3
//app interface
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('ROOT_PATH', dirname(__FILE__));
define('FRAMEWORK_PATH', ROOT_PATH . '/framework');
define('PROTECTED_PATH', ROOT_PATH . '/protected');
define('APP_PATH', PROTECTED_PATH . '/api');
define('MODEL_PATH', PROTECTED_PATH . '/model');
define('TEMPLATE_PATH', PROTECTED_PATH . '/template/wap');
define('UPLOAD_PATH', '/uploadfiles');
require_once(FRAMEWORK_PATH . '/class/request/request.php');
require_once(PROTECTED_PATH . '/config.php');
require_once(FRAMEWORK_PATH . '/global.php');
require_once(FRAMEWORK_PATH . '/wenyan.php');
require_once(APP_PATH . '/core.php');
require_once(MODEL_PATH . '/base.model.php');
require_once(FRAMEWORK_PATH . '/class/smarty-3.1.18/libs/Smarty.class.php');
require_once(FRAMEWORK_PATH . '/class/wxapi/config.php');
require_once(FRAMEWORK_PATH . '/class/wxapi/jssdk.php');
require_once(FRAMEWORK_PATH . '/class/rsa/rsa.php'); //rsa数据加密

$isRSA = false; //供success函数输出值类型判断用
if (IS_POST) $_POST = getDatas(); //RSA加密获取数据(兼容非RSA传输)

if (!get_magic_quotes_gpc()) {
    $_GET   = addslashes_deep($_GET);
    $_POST  = addslashes_deep($_POST);
    $_COOKIE = addslashes_deep($_COOKIE);
    $_SERVER = addslashes_deep($_SERVER);
}
$request = new Request();

//初始化smarty
$smarty = new Smarty();
$smarty->setTemplateDir(TEMPLATE_PATH);
$smarty->setCompileDir(ROOT_PATH . '/temp/templates_c/wap/');
$smarty->setCacheDir(ROOT_PATH . '/temp/cache/wap/');

//default return json array
$json = array(
	"data" => null,
	"msg_type" => 0,
	"msg" => "成功",
	"error" => 0
);

$app = isset($_GET['app']) ? strtolower(trim($_GET['app'])) : "home";
$act = isset($_GET['act']) ? strtolower(trim($_GET['act'])) : "index";
//security check file and function
if (preg_match("/^[a-zA-Z0-9_.-]+$/", $app) && preg_match("/^[a-zA-Z0-9_.-]+$/", $act)) {
	$file = APP_PATH."/{$app}.php";
	if (file_exists($file)) {
		require_once($file);
		if (class_exists($app)) {
		    $class = new $app();
		    if (method_exists($class, $act)) {
		    	$class->$act();
		    } else {
		    	error('Missing method');
		    }
		} else {
			error('Missing controller');
		}
	} else {
		error('Missing file');
	}
} else {
	error('Wrong file');
}

//trigger error, exit
function error($msg, $msg_type=0, $error=1) {
	global $json;
	$json['error'] = $error;
	$json['msg_type'] = $msg_type;
	$json['msg'] = $msg;

	$gourl = (isset($_REQUEST['gourl']) && trim($_REQUEST['gourl'])) ? trim($_REQUEST['gourl']) : '';
	if (strlen($gourl)) {
		$html = "<meta charset=\"UTF-8\"><script>alert('{$msg}');history.back();</script>";
		exit($html);
	}

	die(json_encode($json));
}

//success and exit
function success($data, $msg='成功', $msg_type=0, $element=array(), $gourl='', $goalert='') {
	global $json, $db, $tbp, $isRSA;
	$json['data'] = $data;
	$json['msg_type'] = $msg_type;
	$json['msg'] = $msg;

	if (isset($_SESSION['member'])) {
		$member_id = $_SESSION['member']->id;
		$sql = "SELECT count(*) FROM {$tbp}message WHERE member_id='{$member_id}' AND readed=0";
		$num = intval($db->get_var($sql));
		$json['member_notify'] = $num;
	}

	if (is_array($element)) foreach ($element as $key=>$val) $json[$key] = $val;
	
	$is_app = stripos($_SERVER['HTTP_USER_AGENT'], 'ruandao')===false ? 0 : 1;
	$is_wx = stripos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')===false ? 0 : 1;
	$is_mini = (isset($_REQUEST['is_mini']) && intval($_REQUEST['is_mini']) && $is_wx) ? intval($_REQUEST['is_mini']) : 0;
	$gourl = (isset($_REQUEST['gourl']) && trim($_REQUEST['gourl'])) ? trim($_REQUEST['gourl']) : $gourl;
	$goalert = (isset($_REQUEST['goalert']) && trim($_REQUEST['goalert'])) ? trim($_REQUEST['goalert']) : $goalert;
	if (strlen($gourl)) {
		$html = '<meta charset="UTF-8"><script>';
		if (strlen($goalert)) $html .= "alert('{$goalert}');";
		$html .= "location.href = '{$gourl}';</script>";
		exit($html);
	}

	//$str = str_replace(":[]", ":\"\"", str_replace(":null", ":\"\"", json_encode($json)));
	//$str = str_replace(":null", ":\"\"", json_encode($json));
	$str = str_replace(":[]", ":null", json_encode($json));

	if (RSA_POST && $isRSA) {
		$rsa = new Rsa(FRAMEWORK_PATH . '/class/rsa/keys', "{$tbp}private", "{$tbp}public");
		$str = $rsa->privEncode($str);
	}

	die($str);
}
